package pl.edu.pk.mech.kkbus.service;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import pl.edu.pk.mech.kkbus.component.MyEntityManagerFactory;

import pl.edu.pk.mech.kkbus.entities.Bus;
import pl.edu.pk.mech.kkbus.entities.Defect;
import pl.edu.pk.mech.kkbus.entities.Refueling;

@Service
public class BusService {
    
    private static final Logger log = Logger.getLogger(BusService.class.getName());
    
    EntityManagerFactory emf = MyEntityManagerFactory.get();
    
    public List<pl.edu.pk.mech.kkbus.dto.Bus> getAllBusses() {
        List<pl.edu.pk.mech.kkbus.dto.Bus> result = 
                new LinkedList<pl.edu.pk.mech.kkbus.dto.Bus>();
        EntityManager em = null;
        
        try {
            em = emf.createEntityManager();
            Query query = em.createQuery("SELECT b FROM Bus b ORDER BY b.brand");
            List<Bus> results = (List<Bus>) query.getResultList();
            Iterator<Bus> iterator = results.iterator();
            while (iterator.hasNext()) {                
                Bus b = iterator.next();
                result.add(new pl.edu.pk.mech.kkbus.dto.Bus(b.getKey().getId(),
                        b.getBrand(), b.getDescription(), b.getPlaces(),
                        b.getRegistartion(), b.getParking(), b.getYear()));
            }
            
            return result;
        } catch(NoResultException e) {
            return null;
        } finally {
            if (em != null)
                em.close();
        }
    } 
    
    public List<pl.edu.pk.mech.kkbus.dto.Defect> getDefectForBus(long id) {
        List<pl.edu.pk.mech.kkbus.dto.Defect> result = 
                new LinkedList<pl.edu.pk.mech.kkbus.dto.Defect>();
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Query query = em.createQuery("SELECT d FROM Defect d WHERE d.busId = :busId");
            query.setParameter("busId", id);
            List<Defect> results = (List<Defect>) query.getResultList();
            Iterator<Defect> iterator = results.iterator();
            while (iterator.hasNext()) {  
                Defect d = iterator.next();
                
                result.add(new pl.edu.pk.mech.kkbus.dto.Defect(d.getKey().getId(),
                        d.getDescription(), d.getCost(), d.getDate()));
            }
            return result;         
        } catch(NoResultException e) {
            return null;
        } finally {
            if (em != null)
                em.close();
        }
    }
  
    public List<pl.edu.pk.mech.kkbus.dto.Refueling> getRefuelingForBus(long id) {
        List<pl.edu.pk.mech.kkbus.dto.Refueling> result = 
                new LinkedList<pl.edu.pk.mech.kkbus.dto.Refueling>();
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Query query = em.createQuery("SELECT r FROM Refueling r WHERE r.busId = :busId");
            query.setParameter("busId", id);
            List<Refueling> results = (List<Refueling>) query.getResultList();
            Iterator<Refueling> iterator = results.iterator();
            while (iterator.hasNext()) {  
                Refueling r = iterator.next();
                
                result.add(new pl.edu.pk.mech.kkbus.dto.Refueling(r.getKey().getId(),
                        r.getLiters(), r.getCosts(), r.getDate()));
            }
            return result;         
        } catch(NoResultException e) {
            return null;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean create(pl.edu.pk.mech.kkbus.dto.Bus b) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Bus bus = new Bus(b.getBrand(), b.getDescription(), b.getPlaces(),
                    b.getRegistartion(), b.getParking(), b.getYear());
            em.persist(bus);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean update(long id, pl.edu.pk.mech.kkbus.dto.Bus b) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Bus bus = em.find(Bus.class, new Long(id));
            bus.setBrand(b.getBrand());
            bus.setDescription(b.getDescription());
            bus.setParking(b.getParking());
            bus.setPlaces(b.getPlaces());
            bus.setRegistartion(b.getRegistartion());
            bus.setYear(b.getYear());
            
            em.persist(bus);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean delete(long id) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Bus bus = em.find(Bus.class, new Long(id));
            em.remove(bus);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }    
}
